package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.project.api.dto.AddOrdersInfoGoodsDto;
import com.qinggeng.project.system.domain.Goods;
import com.qinggeng.project.system.domain.GoodsPercent;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.mapper.GoodsPercentMapper;
import com.qinggeng.project.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.List;

import static com.qinggeng.project.system.service.impl.GiftServiceImpl.DRAW_TIMES_KEY;

@Service
@Slf4j
public class GoodsPercentServiceImpl implements IGoodsPercentService {

    @Resource
    private GoodsPercentMapper goodsPercentMapper;
    @Autowired
    private IGiftService giftService;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private MQSendService mqSendService;
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Autowired
    private TransactionDefinition definition;

    @Override
    public boolean insertGoodsPercent(GoodsPercent goodsPercent) {
        int rows=0;
        TransactionStatus status=null;
        try{
            status=transactionManager.getTransaction(definition);
            rows=goodsPercentMapper.insertGoodsPercent(goodsPercent);
            transactionManager.commit(status);
        }catch (Exception e){
            transactionManager.rollback(status);
        }
        if(rows<1){
            return false;
        }
        mqSendService.sendGoodsPercentageDelayMessage(goodsPercent.getClassifyId());
        return true;
    }

    public boolean updateGoodsPercentage(Long classifyId){
        if(null==classifyId){
            throw new CustomException("classifyId is null");
        }
        Goods where=new Goods();
        where.setClassifyId(classifyId);
        List<Goods> goodsList = goodsService.selectGoodsList(where);
        for (Goods goods:goodsList) {
            if(goods.getBuyPrice()!=null&&goods.getPercent()!=null){
                continue;
            }
            goodsService.updateGoodsPercentage(goods);
        }
        return true;
    }

    @Override
    public int deleteGoodsPercentByIds(Long[] ids) {
        return goodsPercentMapper.deleteGoodsPercentByIds(ids);
    }

    @Override
    public boolean updateGoodsPercent(GoodsPercent goodsPercent) {
        int rows=0;
        TransactionStatus status=null;
        try{
            status=transactionManager.getTransaction(definition);
            rows=goodsPercentMapper.updateGoodsPercent(goodsPercent);
            transactionManager.commit(status);
        }catch (Exception e){
            transactionManager.rollback(status);
        }
        if(rows<1){
            return false;
        }
        mqSendService.sendGoodsPercentageDelayMessage(goodsPercent.getClassifyId());
        return true;
    }

    @Override
    public List<GoodsPercent> selectGoodsPercentList(GoodsPercent goodsPercent) {
        return goodsPercentMapper.selectGoodsPercentList(goodsPercent);
    }

    @Override
    public GoodsPercent getGoodsPercentById(Long id) {
        return goodsPercentMapper.getGoodsPercentById(id);
    }

    @Override
    public GoodsPercent selectGoodsPercentByGoodsId(Long goodsId) {
        return goodsPercentMapper.selectGoodsPercentByGoodsId(goodsId);
    }



    @Override
    public Integer getDrawTimes(Orders orders){
        BigDecimal drawTimes=new BigDecimal(0);
        try{
            for (GoodsPercent goodsPercent:selectGoodsPercentList(null) ) {
                if(null==goodsPercent.getDrawPrice()){
                    continue;
                }
                drawTimes=drawTimes.add(goodsPercentMapper.getDrawTimes(orders.getOrdersId(),goodsPercent.getClassifyId(),goodsPercent.getDrawPrice()));
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        BigDecimal maxDrawTimes=new BigDecimal(sysConfigService.getConfigValue(DRAW_TIMES_KEY));
        if(drawTimes.compareTo(maxDrawTimes)>0){
            return maxDrawTimes.intValue();
        }
        return drawTimes.intValue();
    }


}
